<?php
class calcule_administrative_mdl extends CI_Model
{
	
	public function get_all()
	{

 $this->load->library('session');
 $year=$this->session->userdata('annee');
		 
	$query = $this->db->query("SELECT
*
FROM
budget
WHERE
budget.annee = $year

 ");

return $query->result();
     

	
	}
    
	public function calcule_chapitre($num_ch)
	{

 $this->load->library('session');
 $year=$this->session->userdata('annee');
		 
	$query = $this->db->query("SELECT
bc_produits.quantite,
bc_produits.bc_produit_pu
FROM
engagements
INNER JOIN bcs ON bcs.id_engagement_fk = engagements.id_engagement
INNER JOIN bc_produits ON bc_produits.id_bc_fk = bcs.id_bc
INNER JOIN articles ON engagements.id_article_fk = articles.id_article AND '' = ''
INNER JOIN chapitres ON articles.id_chapitre_fk = chapitres.id_chapitre
WHERE
chapitres.id_chapitre = '$num_ch' AND
engagements.annee = '$year'

 ");

return $query->result();
   

	
	}
	
	
	public function calcule_chapitre_convention($num_ch)
	{

 $this->load->library('session');
 $year=$this->session->userdata('annee');
		 
	$query = $this->db->query("SELECT
convention_produits.id_produit_fk,
convention_produits.quantite,
convention_produits.pu_convention_produit
FROM
engagements
INNER JOIN articles ON engagements.id_article_fk = articles.id_article AND ''= ''
INNER JOIN chapitres ON articles.id_chapitre_fk = chapitres.id_chapitre
INNER JOIN conventions ON conventions.id_article_fk = articles.id_article AND conventions.id_engagement_fk = engagements.id_engagement
INNER JOIN convention_produits ON convention_produits.id_convention_fk = conventions.id_convetion
WHERE
chapitres.id_chapitre = '$num_ch' AND
engagements.annee = '$year'

 ");

return $query->result();
   

	
	}
 
    	public function calcule_article($num_ch,$code_art)
	{

 $this->load->library('session');
 $year=$this->session->userdata('annee');
 $chapitre='ch_'.$num_ch;	 
	$query = $this->db->query("SELECT
articles.nom,
bc_produits.quantite,
bc_produits.bc_produit_pu,
articles.`code`
FROM
articles
INNER JOIN engagements ON engagements.id_article_fk = articles.id_article
INNER JOIN bcs ON bcs.id_engagement_fk = engagements.id_engagement
INNER JOIN bc_produits ON bc_produits.id_bc_fk = bcs.id_bc
WHERE
articles.id_chapitre_fk = $num_ch AND
articles.`code` = $code_art and
engagements.annee = $year

 ");

return $query->result();
   

	
	}

    	public function calcule_article_convention($num_ch,$code_art)
	{

 $this->load->library('session');
 $year=$this->session->userdata('annee');
 $chapitre='ch_'.$num_ch;	 
	$query = $this->db->query("SELECT
articles.nom,
articles.`code`,
convention_produits.id_produit_fk,
convention_produits.quantite,
convention_produits.pu_convention_produit
FROM
articles
INNER JOIN engagements ON engagements.id_article_fk = articles.id_article
INNER JOIN conventions ON conventions.id_article_fk = articles.id_article AND conventions.id_engagement_fk = engagements.id_engagement
INNER JOIN convention_produits ON convention_produits.id_convention_fk = conventions.id_convetion
WHERE
articles.id_chapitre_fk = $num_ch AND
articles.`code` = $code_art and
engagements.annee = $year
 ");

return $query->result();
   

	
	}

    public function get_articles_solde($num_ch,$init)
	{

 $this->load->library('session');
 $year=$this->session->userdata('annee');
		 
	$query = $this->db->query("SELECT

budget_art.solde
FROM
articles
INNER JOIN budget_art ON budget_art.id_art_fk = articles.id_article
INNER JOIN chapitres ON articles.id_chapitre_fk = chapitres.id_chapitre
WHERE
articles.id_chapitre_fk = $num_ch AND
budget_art.annee = $year AND
budget_art.init = $init


 ");


return $query->result();
     

	
	}

      public function calcule_chapitre_transfert($num_ch)
	{

 $this->load->library('session');
 $year=$this->session->userdata('annee');
		 
	$query = $this->db->query("SELECT
transfert.id_tranfert,
transfert.article_source,
transfert.article_dest,
transfert.montant,
transfert.`year`,
transfert.date,
transfert.numero,
chapitres.nom,
chapitres.id_chapitre,
source.`code` AS code_source,
destination.`code` AS code_destination
FROM
transfert
INNER JOIN articles AS source ON transfert.article_dest = source.id_article
INNER JOIN articles AS destination ON transfert.article_source = destination.id_article
INNER JOIN chapitres ON source.id_chapitre_fk = chapitres.id_chapitre AND destination.id_chapitre_fk = chapitres.id_chapitre
WHERE
chapitres.id_chapitre = $num_ch AND
transfert.`year` = $year

 ");


return $query->result();
     

	
	}
	
	 public function calcule_article_transfert_source($num_ch,$code_source)
	{

 $this->load->library('session');
 $year=$this->session->userdata('annee');
		 
	$query = $this->db->query("SELECT
transfert.id_tranfert,
transfert.article_source,
transfert.article_dest,
transfert.montant,
transfert.`year`,
transfert.date,
transfert.numero,
chapitres.nom,
chapitres.id_chapitre,
destination.`code` AS code_destination,
source.`code` AS code_source
FROM
transfert
INNER JOIN articles AS destination ON transfert.article_dest = destination.id_article
INNER JOIN articles AS source ON transfert.article_source = source.id_article
INNER JOIN chapitres ON destination.id_chapitre_fk = chapitres.id_chapitre AND source.id_chapitre_fk = chapitres.id_chapitre
WHERE
chapitres.id_chapitre = $num_ch AND
transfert.`year` = $year	AND
source.`code` = '$code_source'

 ");


return $query->result();
     

	
	}
	
	 public function calcule_article_transfert_destination($num_ch,$code_dest)
	{

 $this->load->library('session');
 $year=$this->session->userdata('annee');
		 
	$query = $this->db->query("SELECT
transfert.id_tranfert,
transfert.article_source,
transfert.article_dest,
transfert.montant,
transfert.`year`,
transfert.date,
transfert.numero,
chapitres.nom,
chapitres.id_chapitre,
destination.`code` AS code_destination,
source.`code` AS code_source
FROM
transfert
INNER JOIN articles AS destination ON transfert.article_dest = destination.id_article
INNER JOIN articles AS source ON transfert.article_source = source.id_article
INNER JOIN chapitres ON destination.id_chapitre_fk = chapitres.id_chapitre AND source.id_chapitre_fk = chapitres.id_chapitre
WHERE
chapitres.id_chapitre = $num_ch AND
transfert.`year` = $year	AND
destination.`code` = '$code_dest'

 ");


return $query->result();
     

	
	}
}

?>